$(function () {
  //1.自定义校验规则
  let form = layui.form
  form.verify({
    //规则1
    pwd: [/^[\S]{6,12}$/, '密码必须2到16位，且不能出现空格'],
    //规则2
    newPwd: function (value) {
      let oldPwd = $('[name="oldPwd"]').val()
      if (value == oldPwd) {
        return '新密码不能与原密码相同!'
      }
    },
    //规则3
    rePwd: function (value) {
      let newPwd = $('[name="newPwd"]').val()
      if (value != newPwd) {
        return '两次密码不一致!'
      }
    },
  })

  //2.修改密码提交
  $('#formPwd').on('submit', function (e) {
    e.preventDefault()
    axios({
      method: 'post',
      url: '/my/updatepwd',
      data: $(this).serialize(),
    }).then(res => {
      if (res.data.status !== 0) {
        return layui.layer.msg(res.data.message)
      }

      //成功提示
      layui.layer.msg('恭喜您，修改密码成功!')
      $('#formPwd')[0].reset()

      //页面跳转
      setTimeout(() => {
        window.parent.location.href = '/login.html'
      }, 1000)
    })
  })
})
